package com.hsav.kcxb.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hsav.kcxb.R;

/**
 * Created by LiB on 2017/6/27.
 */

public class MusicClipHandler extends FrameLayout implements View.OnTouchListener {
    private static final String TAG = MusicClipHandler.class.getName();
    private String title;
    private View content;
    private float lastY;
    private float deltaY;

    public MusicClipHandler(Context context) {
        super(context);
        title = "开始";
        initUI();
    }

    public MusicClipHandler(Context context, AttributeSet attrs) {
        super(context, attrs);
        initField(context, attrs);
        initUI();
    }

    private void initField(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MusicClipHandler);
        title = typedArray.getString(R.styleable.MusicClipHandler_title);
    }

    public MusicClipHandler(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initField(context, attrs);
        initUI();
    }

    private void initUI()
    {
        content = LayoutInflater.from(getContext()).inflate(R.layout.content_music_clip_start_handler, this, true);
        TextView thumb = (TextView) content.findViewById(R.id.thumb);
        thumb.setText(title);
        thumb.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                lastY = event.getRawY();
                Log.d(TAG, String.format("开始拖拽，起始y值：%s", lastY));
                break;
            case MotionEvent.ACTION_MOVE:
                deltaY = event.getRawY() - lastY;
                Log.d(TAG, String.format("正在拖拽，deltaY值：%s", deltaY));
                FrameLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                params.topMargin += (int) deltaY;
                setLayoutParams(params);
                lastY = event.getRawY();
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d(TAG, "取消");
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "抬起");
                break;
        }
        return true;
    }
}
